package
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Cubic;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class DDClone extends Sprite implements ITileGame
	{
		public static const TILE_SIZE:Number = 30;
		public static const WIDTH:int = 8;
		public static const HEIGHT:int = 12;
		
		public static const COLORS:Array = [0xff0000,0x00ff00,0x0000ff,0xff00ff,0xffff00];
		private var m_stage:Stage;
		private var m_allTiles:Vector.<Tile>;
		private var m_helper:TileGameHelper;
		
		public function DDClone(stage:Stage)
		{
			super();
			m_helper = new TileGameHelper(this);
			m_stage = stage;
			m_allTiles = new Vector.<Tile>;
			// support autoOrients
			stage.frameRate = 60;
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			addEventListener(Event.ADDED_TO_STAGE, function(e:Event):void{
				start();
			});
		}
		
		public function addTile(tile:Tile):void
		{
			m_allTiles.push(tile);
			addChild(tile.display);			
		}
		
		public function getBoardWidth():int
		{
			return WIDTH;
		}
		
		public function getBoardHeight():int
		{
			return HEIGHT;
		}
		
		
		private function onMouseDown(me:MouseEvent):void
		{
			var tile:Tile = m_helper.tileHitTest(me.stageX, me.stageY, stage);
			if(tile)
			{
				evaluateTile(tile);
			}					
		}
		
		private function evaluateTile(tile:Tile):void
		{
			var connectedTiles:Vector.<Tile> = m_helper.getConnectedTiles(tile, new Vector.<Tile>);
			if(connectedTiles.length >= 3)
			{
				scoreTiles(connectedTiles);
			}
		}
		
		
		public function createNewTile(x:int,y:int):Tile
		{
			return Tile.createNewTile(x, y, TILE_SIZE, COLORS);
		}

		public function getTileAt(x:int, y:int):Tile
		{
			return m_helper.getTileAt(x, y);
		}
		
		private function start():void
		{
			
			initialTileDrop(function():void{
				stage.addEventListener(Event.ENTER_FRAME, onFrame);
			});
		}
		
		private function initialTileDrop(onComplete:Function):void
		{
			var tiles:Vector.<Tile> = new Vector.<Tile>;
			for(var i:int = 0, il:int = WIDTH; i < il; i++)
			{
				for(var j:int = 0, jl:int = HEIGHT; j < jl; j++)
				{
					var tile:Tile = createNewTile(i,j);
					tiles.push(tile);
					tile.display.y -= DDClone.HEIGHT*tile.tileSize;
					m_allTiles.push(tile);
					addChild(tile.display);
				}
			}
			
			var tdg:TileDropGroup = new TileDropGroup(tiles);
			tdg.dropStyle = TileDropGroup.DROP_STYLE_BULK_NEW;
			tdg.go(onComplete());
			
		}
		
		private function scoreTiles(tiles:Vector.<Tile>):void
		{
			
			m_helper.destroyTiles(tiles);
		}		
		
		private function dropNewTiles():void
		{
			m_helper.dropNewTiles();
		}
		
		public function getAllTiles():Vector.<Tile>
		{
			return m_allTiles;
		}		
		
		private function onFrame(e:Event):void
		{
			m_helper.onFrame();
		}
		
	}
}